/*
date:20210520 21:34PM
key:经典二元组排序第二个
*/
class Solution {
public:
    static bool f(const pair<string,int>p1,const pair<string,int>p2)
    {
        if(p1.second==p2.second)
        {
            return p1.first<p2.first;
        }
        return p1.second>p2.second;
    }
    vector<string> topKFrequent(vector<string>& words, int k) 
    {
        map<string,int>m;
        for(string&s:words)
        {
            auto k=m.find(s);
            if(k==m.end())
            {
                m.insert(pair<string,int>(s,1));
            }
            else
            {
                k->second++;
            }
        }
        vector<pair<string,int>>v1;
        v1.resize(m.size());
        int i=0;
        for(pair<string,int>a:m)
        {  
            v1[i]=pair<string,int>(a.first,a.second);
            i++;
        }
        sort(v1.begin(),v1.end(),f);
        vector<string>v;
        v.resize(k);
        i=0;
        for(pair<string,int>a:v1)
        {  
            v[i]=a.first;
            i++;
        }
        return v;
    }
};